#include "fight.h"

#include <gl/gl.h>
#include "InputManager/keyboardmanager.h"
#include "random.h"
#include "statemanager.h"
#include "graphicsmanager.h"

#define player StateManager::Instance->FightingPlayer
#define enemy StateManager::Instance->FightingEnemy

Texture* Fight::battleground_texture = nullptr;

Fight::Fight() : CurrentTurn(Turns::Player), FightCursor(FightOptions::Attack), MagicCursor(0), DisplayMagic(false) {
    if (battleground_texture == nullptr) {
        battleground_texture = new Texture("battle.png");
    }
}

Fight::~Fight() {
}

bool Fight::AcceptPressed(){
	return KeyboardManager::IsPressed(VK_RETURN) || KeyboardManager::IsPressed(VK_SPACE);
}

void Fight::Update(){
    if (CurrentTurn == Turns::Player) {
        if (FightCursor == FightOptions::Attack) {
            if (KeyboardManager::IsPressed(VK_DOWN)) {
                FightCursor = FightOptions::Magic;
            }
            if (AcceptPressed()) {
                player->attack(enemy);
                CurrentTurn = Turns::Enemy;
            }
        }
        else {
            if (KeyboardManager::IsPressed(VK_UP) && !DisplayMagic) {
                FightCursor = FightOptions::Attack;
            }
            if (AcceptPressed() && DisplayMagic && player->Spells.size() && (player->MP.Current > player->Spells[MagicCursor]->mp_cost)) {
                player->Spells[MagicCursor]->cast(player, enemy);
                CurrentTurn = Turns::Enemy;
            }
            if (AcceptPressed() && !DisplayMagic) {
                DisplayMagic = true;
            }
            if (KeyboardManager::IsPressed(VK_ESCAPE) && DisplayMagic) {
                DisplayMagic = false;
            }
            if (KeyboardManager::IsPressed(VK_UP) && DisplayMagic && (MagicCursor > 0)) {
                MagicCursor--;
            }
            if (KeyboardManager::IsPressed(VK_DOWN) && DisplayMagic && player->Spells.size() && (MagicCursor < (player->Spells.size()-1))) {
                MagicCursor++;
            }
        }
        if (enemy->HP.Current <= 0) {
            enemy->Kill();
        }
	}
	else {
	    enemy->turn(player);
	    CurrentTurn = Turns::Player;
	    if (player->HP.Current <= 0) {
            player->Kill();
	    }
	}
	if (!(player->IsAlive()) || !(enemy->IsAlive())) {
        StateManager::Instance->FightOver();
        CurrentTurn = Turns::Player;
        FightCursor = FightOptions::Attack;
        MagicCursor = 0;
        DisplayMagic = false;
	}
}

void Fight::Draw(){
    battleground_texture->Draw(0,0);
    player->texture->Draw(500,350);
    enemy->texture->Draw(200,350);
    GraphicsManager::DrawRectangle(600, 350, 100 * ((1.0f)*player->HP.Current/player->HP.Maximum), 16, Color(1,0,0));
    GraphicsManager::DrawRectangle(600, 366, 100 * ((1.0f)*player->MP.Current/player->MP.Maximum), 16, Color(0,0,1));
    GraphicsManager::DrawRectangle(200, 350, -100 * ((1.0f)*enemy->HP.Current/enemy->HP.Maximum), 16, Color(1,0,0));
    GraphicsManager::DrawRectangle(200, 366, -100 * ((1.0f)*enemy->MP.Current/enemy->MP.Maximum), 16, Color(0,0,1));
    if (CurrentTurn == Turns::Player) {
        GraphicsManager::DrawRectangle(300, 500, 16 * 10, 16 * 2,  Color(0,0,0));
        GraphicsManager::DrawString(300+16, 500, "Attack");
        GraphicsManager::DrawString(300+16, 500+16, "Magic");
        GraphicsManager::DrawString(300, 500 + (int)FightCursor*16, ">");
        if (DisplayMagic) {
            if (!player->Spells.size()) {
                GraphicsManager::DrawRectangle(300+(16*10), 500, 16*10, 16, Color(0,0,0));
                GraphicsManager::DrawString(300+(16*11), 500, "No spells!");
            }
            else {
                GraphicsManager::DrawRectangle(300+(16*10), 500, 16*10, 16 * player->Spells.size(), Color(0,0,0));
                for (unsigned int i = 0; i < player->Spells.size(); i++) {
                    std::string spell = player->Spells[i]->name;
                    spell += " MP Cost: ";
                    spell += std::to_string(player->Spells[i]->mp_cost);
                    GraphicsManager::DrawString(300 + (16*11), 500+(16*i), spell);
                }
                GraphicsManager::DrawString(300 + (16*10), 500 + (16*MagicCursor), ">");
            }
        }
    }

}
